package cn.xuetian.spring.framework;

import cn.xuetian.spring.framework.component.X;
import cn.xuetian.spring.framework.component.Y;
import cn.xuetian.spring.framework.component.Z;
import cn.xuetian.spring.framework.service.UserService;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@SpringBootApplication
public class Starter {

    public static void main(String[] args) {

        //run() ==> AnnotationConfigApplicationContext
        ConfigurableApplicationContext context = SpringApplication.run(Starter.class, args);


        context = new ClassPathXmlApplicationContext("config.xml");

        //context = new AnnotationConfigApplicationContext();

        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(UserService.class);
        UserService userService = new UserService();
        beanDefinitionBuilder.addPropertyValue("userService",userService);
        beanFactory.registerSingleton("userService",userService);

        System.out.println("server start up done...");


//        ApplicationStartup applicationStartup = context.getApplicationStartup();
//
//
//        context.addBeanFactoryPostProcessor(configurableListableBeanFactory -> {
//
//            X bean = configurableListableBeanFactory.getBean(X.class);
//
//            Y bean2 = configurableListableBeanFactory.getBean(Y.class);
//
//        });


        X xBean = context.getBean(X.class);
//
        Y yBean = context.getBean(Y.class);

        Z zBean = context.getBean(Z.class);

        UserService bean = context.getBean(UserService.class);

        System.out.println(zBean);

    }
}
